
package network.server.handler.BankHandler;

import module.bank.BankAccountService;
import module.bank.impl.BankAccountServiceImpl;
import network.protocol.Request;
import network.protocol.Response;
import network.server.RequestHandler;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.Map;

public class BankAccountHandler implements RequestHandler {
    private final BankAccountService bankAccountService;

    public BankAccountHandler() {
        // 使用单例模式获取BankAccountService实例
        this.bankAccountService = BankAccountServiceImpl.getInstance();
    }

    @Override
    public Response<?> handle(Request req) {
        // 获取请求参数
        Map<String, Object> params = req.getParams();
        String action = req.getAction();
        action = action.substring(action.indexOf('.') + 1);

        try {
            // 根据action参数执行不同的操作
            switch (action) {
                case "getAccountsByUserId":
                    return handleGetAccountsByUserId(params);

                case "getAccountById":
                    return handleGetAccountById(params);

                case "recharge":
                    return handleRecharge(params);

                case "withdraw":
                    return handleWithdraw(params);

                case "transfer":
                    return handleTransfer(params);

                case "getTransactionHistory":
                    return handleGetTransactionHistory(params);

                case "createAccount":
                    return handleCreateAccount(params);

                case "updateAccountName":
                    return handleUpdateAccountName(params);

                case "deleteAccount":
                    return handleDeleteAccount(params);


                default:
                    return Response.error("未知操作: " + action);
            }
        } catch (NumberFormatException e) {
            return Response.error("参数格式错误: " + e.getMessage());
        } catch (Exception e) {
            return Response.error("处理请求时发生错误: " + e.getMessage());
        }
    }

    // 获取用户账户列表
    private Response<?> handleGetAccountsByUserId(Map<String, Object> params) {
        if (!params.containsKey("userId")) {
            return Response.error("缺少必要参数: userId");
        }

        try {
            long userId = Long.parseLong(params.get("userId").toString());
            return bankAccountService.getAccountsByUserId(userId);
        } catch (NumberFormatException e) {
            return Response.error("userId参数格式错误");
        }
    }

    // 获取账户信息
    private Response<?> handleGetAccountById(Map<String, Object> params) {
        if (!params.containsKey("accountId")) {
            return Response.error("缺少必要参数: accountId");
        }

        try {
            long accountId = Long.parseLong(params.get("accountId").toString());
            return bankAccountService.getAccountById(accountId);
        } catch (NumberFormatException e) {
            return Response.error("accountId参数格式错误");
        }
    }

    // 账户充值
    private Response<?> handleRecharge(Map<String, Object> params) {
        if (!params.containsKey("accountId") || !params.containsKey("amount")) {
            return Response.error("缺少必要参数: accountId 或 amount");
        }

        try {
            long accountId = Long.parseLong(params.get("accountId").toString());
            BigDecimal amount = new BigDecimal(params.get("amount").toString());

            return bankAccountService.recharge(accountId, amount);
        } catch (NumberFormatException e) {
            return Response.error("参数格式错误: accountId必须为数字, amount必须为有效金额");
        } catch (Exception e) {
            return Response.error("充值操作失败: " + e.getMessage());
        }
    }

    // 修改账户名
    private Response<?> handleUpdateAccountName(Map<String, Object> params) {
        if (!params.containsKey("accountId") || !params.containsKey("newName")) {
            return Response.error("缺少必要参数: accountId 或 newName");
        }
        try {
            long accountId = Long.parseLong(params.get("accountId").toString());
            String newName = params.get("newName").toString();
            return bankAccountService.updateAccountName(accountId, newName);
        } catch (NumberFormatException e) {
            return Response.error("参数格式错误: accountId必须为数字");
        }
    }

    // 删除账户
    private Response<?> handleDeleteAccount(Map<String, Object> params) {
        if (!params.containsKey("accountId")) {
            return Response.error("缺少必要参数: accountId");
        }
        try {
            long accountId = Long.parseLong(params.get("accountId").toString());
            return bankAccountService.deleteAccount(accountId);
        } catch (NumberFormatException e) {
            return Response.error("参数格式错误: accountId必须为数字");
        }
    }


    // 账户取款
    private Response<?> handleWithdraw(Map<String, Object> params) {
        if (!params.containsKey("accountId") || !params.containsKey("amount")) {
            return Response.error("缺少必要参数: accountId 或 amount");
        }

        try {
            long accountId = Long.parseLong(params.get("accountId").toString());
            BigDecimal amount = new BigDecimal(params.get("amount").toString());

            return bankAccountService.withdraw(accountId, amount);
        } catch (NumberFormatException e) {
            return Response.error("参数格式错误: accountId必须为数字, amount必须为有效金额");
        } catch (Exception e) {
            return Response.error("取款操作失败: " + e.getMessage());
        }
    }

    // 转账操作
    private Response<?> handleTransfer(Map<String, Object> params) {
        if (!params.containsKey("fromAccountId") || !params.containsKey("toAccountId") ||
                !params.containsKey("amount")) {
            return Response.error("缺少必要参数: fromAccountId, toAccountId 或 amount");
        }

        try {
            long fromAccountId = Long.parseLong(params.get("fromAccountId").toString());
            long toAccountId = Long.parseLong(params.get("toAccountId").toString());
            BigDecimal amount = new BigDecimal(params.get("amount").toString());
            String description = params.getOrDefault("description", "转账").toString();

            return bankAccountService.transfer(fromAccountId, toAccountId, amount, description);
        } catch (NumberFormatException e) {
            return Response.error("参数格式错误: 账户ID必须为数字, amount必须为有效金额");
        } catch (Exception e) {
            return Response.error("转账操作失败: " + e.getMessage());
        }
    }

    // 获取交易记录
    private Response<?> handleGetTransactionHistory(Map<String, Object> params) {
        if (!params.containsKey("accountId")) {
            return Response.error("缺少必要参数: accountId");
        }

        try {
            long accountId = Long.parseLong(params.get("accountId").toString());
            LocalDateTime startDate = null;
            LocalDateTime endDate = null;
            Integer transactionType = null;

            // 可选参数处理
            if (params.containsKey("startDate")) {
                startDate = LocalDateTime.parse(params.get("startDate").toString());
            }

            if (params.containsKey("endDate")) {
                endDate = LocalDateTime.parse(params.get("endDate").toString());
            }

            if (params.containsKey("transactionType")) {
                transactionType = Integer.parseInt(params.get("transactionType").toString());
            }

            return bankAccountService.getTransactionHistory(accountId, startDate, endDate, transactionType);
        } catch (NumberFormatException e) {
            return Response.error("参数格式错误: accountId必须为数字");
        } catch (Exception e) {
            return Response.error("获取交易记录失败: " + e.getMessage());
        }
    }

    // 创建账户
    private Response<?> handleCreateAccount(Map<String, Object> params) {
        if (!params.containsKey("userId")) {
            return Response.error("缺少必要参数: userId");
        }
        if (!params.containsKey("accountName")) {
            return Response.error("缺少必要参数: accountName");
        }

        try {
            long userId = Long.parseLong(params.get("userId").toString());
            String accountName = params.get("accountName").toString();

            return bankAccountService.createAccount(userId, accountName);
        } catch (NumberFormatException e) {
            return Response.error("参数格式错误: userId必须为数字");
        } catch (Exception e) {
            return Response.error("创建账户失败: " + e.getMessage());
        }
    }
}